<?php
    function makeInsert($query) {
        $result = mysql_query($query);
        if (!$result) {
            $message  = 'Invalid query: ' . mysql_error() . "\n";
            $message .= 'Whole query: ' . $query;
            die($message);
        } else {
            return $result;
    }

    function makeSelect($query) {
        $result = mysql_query($query);
        if (!$result) {
            $message  = 'Invalid query: ' . mysql_error() . "\n";
            $message .= 'Whole query: ' . $query;
            die($message);
        }
    }

    function registerUser() {
        makeInsert("INSERT INTO Users (VkUserID, startDate, numberOfEntries) VALUES (3, now(), 10)");
    }

    function checkExistanceForUser($feedID, $userID) {
        $result = mysql_query(" SELECT * FROM FeedsForUsers WHERE feedID=" + $feedID + " AND userID=" + $userID);
        if (!$result) {
            $message  = 'Invalid query: ' . mysql_error() . "\n";
            $message .= 'Whole query: ' . $query;
            die($message);
        }
        $num_rows = mysql_num_rows($result);
        if($num_rows){
            return -1;
        } else {
            return 0;
        }
    }

    // checking Feed global existence
    // ------
    // in:  $url
    // out: "-1"   - if no feed has been found
    //      Feed ID - if Feed is already exists
    function checkFeedExistance($url) {
        $result = mysql_query(" SELECT feedID FROM Feeds WHERE feedURL='" + $url"'");
        if (!$result) {
            $message  = 'Invalid query: ' . mysql_error() . "\n";
            $message .= 'Whole query: ' . $query;
            die($message);
        }
        $num_rows = mysql_num_rows($result);
        if($num_rows){
            return mysql_fetch_row($result)[0];
        } else {
            return -1;
        }
    }

    // add Feed function. DB implementation.
    // ------
    // in: $url    - URL of Feed
    //     $userID - User ID
    // out: Result Code: 0 - success,
    //                   1 - Feed is already exists for the given user
    function addFeedDB($url, $userID) {
        $feedID = checkFeedExistance($url, $userID);
        // check
        if ($feedID == -1) {
            // insert new feed
            $feedID = makeInsert(" INSERT INTO Feeds(feedURL) VALUES ('" + $url + "'");
        } else {
            // check feedID existence for user
            $result = checkFeedExistanceUser($feedID, $userID);
            if ($result == 0) {
                // insert new feed for user
                $name = "'temp'";
                $isAutoUpdate = 1;

                $params = $userID + ", " + $feedID + ", " + $name + ""
                makeInsert(" INSERT INTO FeedsForUsers(userID, feedID, name, isAutoUpdate, autoUpdateInterval, numberOfEntries, order) VALUES '" + $url + ""'");
                return 0;
            } else {
                return 1;
            }
        }
    }

    function getFeedsDB($userID) {

    }

?>